package online.shenme.day15.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Server {
    public static void main(String[] args) throws IOException {
//      method1();
        method2();
    }

    //服务端字符流接收
    private static void method1() throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        BufferedReader br = new BufferedReader(new InputStreamReader(ss.accept().getInputStream()));
        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    }

    //服务端IO流接收
    private static void method2() throws IOException {
        ServerSocket ss0 = new ServerSocket(10000);

        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3, 5, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        FTP_DAO dao = new FTP_DAO();
        while (true) {
            DatagramSocket service = new DatagramSocket(8868);
            byte[] dates = new byte[1024];
            DatagramPacket packet = new DatagramPacket(dates, dates.length);
            service.receive(packet);
            Socket accept = ss0.accept();

            pool.submit(() -> {
                dao.update(accept);
            });
        }


    }
}
